// pages/list/list.js
const fetch = require("../../utils/fetch.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loading: false,
        toview: "twf1",
        activeIndex: 0,
        listData: null,
        box: {
            top: [],
            bottom: []
        },
        name:null,
        tabHovers:1,
        // 商品类型索引值和商品索引值
        shopType:null,
        shopIndex:null,
        //购物车列表
        shopList:[],
        // 购物车商品数量
        shopListNumber:0,
        //购物车商品总价
        shopListPrice:0,
        // 指定那个菜品需要增减
        displayText:[],
        showCarts:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    //获得商品数据
    onLoad: function (options) {
        console.log("进入list");
        wx.showLoading({
            title: "加载中",
            mask: true,
        });
        fetch('food/list').then(res => {
            wx.hideLoading();
            console.log("shiyan");
            this.setData({
                listData: res.data,
                loading: true
            })
        })

    },
    // 获得每个商品类的顶端和低端方位
    onReady: function () {
        // 遍历对象
        var list = this.data.listData.list
        // view底部离顶点的距离
        let height = 0
        let top = []
        let bottom = []
        // console.log(Object.keys(list).length,list);
        for (let index = 1; index < Object.keys(list).length + 1; index++) {
            let selQuery = wx.createSelectorQuery().select("#twf" + index);
            selQuery.fields({
                size: true
            }, (res) => {
                // console.log(res.height);
                top.push(height);
                height += res.height;
                bottom.push(height);
            }).exec()
        }
        // var thst = this
        //     let listData=this.data.listData
        //     console.log(listData);
        //     let listLenght=Object.keys(listData.list).length
        //     for (let i = 0; i < listLenght; i++) {
        //         for (let j = 0; j < Object.keys(listData.list[i+1].food).length; j++) {
        //             let ib="listData.list["+(i+1)+"].food["+(j+1)+"].ifNotNone"
        //             let non="listData.list["+(i+1)+"].food["+(j+1)+"].ifNone" 
        //             thst.setData({
        //                 ib: "inline-block",
        //                 non: "none"
        //             })                   
        //         }
                
        //     }
            console.log(this.data.listData);
        this.setData({
            'box.top': top,
            'box.bottom': bottom,
            name:this.data.listData.list[1].name
        })
    },
    // 选择商品类
    selectItem: function (e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            activeIndex: e.currentTarget.dataset.index,
            toview: 'twf' + index
        })
    },
    // 滑动商品页面时切换商品类的高亮
    scrollLeft: function (e) {
        // 滚动高度
        let scrollTop = e.detail.scrollTop;
        let top = this.data.box.top
        let bottom = this.data.box.bottom
        for (let index = 0; index < top.length; index++) {
            if (scrollTop > top[index]-10 && scrollTop < bottom[index]-10) {
                this.setData({
                    activeIndex: index+1,
                    name:this.data.listData.list[index+1].name,
                    tabHovers:index+1
                })
                break
            }

        }
    },
    // 商品页面添加商品到购物车方法
    addToShopcar:function(e) {
        let type=e.currentTarget.dataset.type;
        let index=e.currentTarget.dataset.index;
        this.setData({shopIndex:index,shopType:type})
        //添加记录
        let addShop={
            indexs:index,
            // 索引
            name:this.data.listData.list[type].food[index].name,
            // 单价
            price:this.data.listData.list[type].food[index].price,
            // 数量
            number:1,
            //该商品的合计总价
            sumPrice:this.data.listData.list[type].food[index].price,
        }
        let shopList=this.data.shopList;
        let displayText=this.data.displayText
        shopList.push(addShop);
        displayText.push(index)
        this.setData({
            shopList:shopList,
            showModalStatus:false,
            shopListPrice:(parseFloat(this.data.shopListPrice)+parseFloat(addShop.price)).toFixed(2),
            shopListNumber:this.data.shopListNumber+1,
            displayText:displayText
        })
        console.log(this.data.shopList);
    },
    // 增加商品数量
    addShopGoods:function(e) {
        let name=e.currentTarget.dataset.name;
        let shopList=this.data.shopList;
        let index=e.currentTarget.dataset.index;
        // 判断是从购物车列表还是商品列表调用该方法
        if (typeof(index)=="undefined") {
           for (let i = 0; i < shopList.length; i++) {
               if ((shopList[i].name)===name) {
                   index=i;
               }
           }
        }
        shopList[index].number++
        shopList[index].sumPrice=shopList[index].sumPrice+shopList[index].price
        this.setData({
            shopList:shopList,
            shopListPrice:shopListPrice+shopList[index].price,
            shopListNumber:this.data.shopListNumber+1
        })
    },
    //减少商品数量
    minusShopGoods:function(e) {
        let name=e.currentTarget.dataset.name;
        let shopList=this.data.shopList;
        let index=e.currentTarget.dataset.index;
        // 判断是从购物车列表还是商品列表调用该方法
        if (typeof(index)=="undefined") {
           for (let i = 0; i < shopList.length; i++) {
               if ((shopList[i].name)===name) {
                   index=i;
               }
           }
        }
        shopList[index].number--
        shopList[index].sumPrice-=shopList[index].price
        price=shopList[index].price
        // 判断购物车中该商品数量是否为空
        if (shopList[index].number<=0) {
            shopList.splice(index,1)
            let displayText=this.data.displayText
            displayText.forEach((v,i,a)=>{
                if(index==v){
                    a.splice(i,1)
                }
            });
            this.setData({
                displayText:displayText
            })
        }
        // 判断购物车列表是否为空
        if (shopList.length<=0) {
            this.setData({
                showCarts=false,
                shopList:[],
                shopListPrice:0,
                shopListNumber:this.data.shopListNumber-1
            })
        }else{
            this.setData({
                shopList:shopList,
                shopListPrice:this.data.shopListPrice-price,
                shopListNumber:this.data.shopListNumber-1
            })
        }
    },
})